/*
 * Copyright (C) 2010-2011 Comcast Cable.  All Rights Reserved.
 */
package com.hercules.android.receiver.handlers.commands.call.animation;

import android.view.animation.ScaleAnimation;
import android.view.animation.Transformation;

import com.hercules.android.receiver.model.objects.XREView;

/**
 * Dimensions animation, it will change dimension of the view.
 * <p>
 * Because method getInvalidateRegion is hidden from public API, we have to make fake scale animation that will make
 * this method return same result as we just increased bounds of the view. But we do not need to apply scale
 * transformation, that is why willChangeTransformationMatrix returns false. Also in applyTransformation we change alpha
 * value a bit in order to trigger transformation clear at every animation step.
 * <p>
 * AndroidAssembly2 this class was added for Android Assembly 2
 *
 * @author 7realm
 * @version 1.0
 */
public class DimensionsAnimation extends ScaleAnimation {
    /** Represents initial width of the view. */
    private final int fromWidth;
    /** Represents initial height of the view. */
    private final int fromHeight;
    /** Represents final width of the view. */
    private final float toWidth;
    /** Represents final height of the view. */
    private final float toHeight;
    /** Represents the modified {@link XREView}. */
    private final XREView xreView;

    /**
     * Constructor for class {@link DimensionsAnimation}.
     *
     * @param xreView the XREView
     * @param toWidth the final width
     * @param toHeight the final height
     */
    public DimensionsAnimation(XREView xreView, float toWidth, float toHeight) {
        super(1, 1, 1, 1);
        this.xreView = xreView;
        this.toHeight = toHeight;
        this.toWidth = toWidth;

        // store initial size
        fromWidth = xreView.getWidth();
        fromHeight = xreView.getHeight();
    }

    /**
     * Applies dimension animation.
     *
     * @param interpolatedTime the interpolated time
     * @param t ignored
     */
    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        super.applyTransformation(interpolatedTime, t);

        // change alpha a bit because this is needed to clear transformation at each step
        //t.setAlpha(0.999f);

        float newWidth = fromWidth + (toWidth - fromWidth) * interpolatedTime;
        float newHeight = fromHeight + (toHeight - fromHeight) * interpolatedTime;

        // update dimensions
        xreView.setDimensions(new int[]{(int) newWidth, (int) newHeight});
        xreView.getAndroidXREView().updateLayout();
    }

    /**
     * Indicates if animation will change it bounds. In our case it changes them.
     *
     * @return always true
     */
    @Override
    public boolean willChangeBounds() {
        return true;
    }

    /**
     * Indicates if transformation matrix should be applied.
     *
     * @return always false, because scale animation is fake
     */
    @Override
    public boolean willChangeTransformationMatrix() {
        return false;
    }
}
